package com.qf.test;

import com.qf.test.util.EsUtil;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

/**
 * 对于索引结构的管理
 * 对于索引的增删改查
 */
public class TsetIndexManage {
    //和es服务器进行 连接的对象
    private RestHighLevelClient client = EsUtil.getClient();
    //常量  索引名字
    private static final String INDEX = "persion";

    /**
     * 创建索引:包含索引的主分片 备份分片 还有文档结构
     */
    @Test
    public void testCreateIndex() throws Exception {
        //1.创建主分片和备份分片
        Settings.Builder settings = Settings.builder()
                .put("number_of_shards", 5)  //主分片
                .put("number_of_replicas", 1);//备份分片

        //2.创建索引结构  包括域名和域的类型
        XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("name")
                .field("type", "text")
                .field("analyzer", "ik_max_word")
                .endObject()

                .startObject("age")
                .field("type", "integer")
                .endObject()

                .startObject("birthday")
                .field("type", "date")
                .field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
                .endObject()
                .endObject()
                .endObject();

        //3.将索引结构和索引分片蛇者封装到一个请求对象中
        CreateIndexRequest request = new CreateIndexRequest(INDEX);
        //将分片 设置放入请求对象中
        request.settings(settings);
        //将结构设置放入请求对象中
        request.mapping(xContentBuilder);
        //4像es服务器发送请求执行创造操作,第二参数 内枚举类型 默认请求类型
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(request);
    }

    /**
     *查看索引是否存在
     */
    @Test
    public void testIndexExists() throws Exception {
        //1.创建get请求对象
        GetAliasesRequest request = new GetAliasesRequest();

        //2.将索引名字放入请求对象中
        request.indices(INDEX);
        //3.查询
        boolean flag = client.indices().existsAlias(request, RequestOptions.DEFAULT);

        //4.返回输出结果
        System.out.println(flag);

    }

    /**
     * 测试删除指定的索引结构和数据
     */
    @Test
    public void testDelteINdex()  throws Exception{
        //1.创建一个删除的请求对象

        DeleteIndexRequest request = new DeleteIndexRequest();
        //2.指定需要删除的索引名字
        request.indices(INDEX);
        //3.发出请求删除
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        //4.返回结果打印
        System.out.println(delete);
    }
}
